package com.qf.gateway.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.qf.common.bean.R;
import com.qf.common.dto.TenantDto;
import com.qf.common.model.QfTenant;
import com.qf.gateway.feign.TenantFeign;
import com.qf.gateway.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author 千锋健哥
 */
@Service
public class TenantServiceImpl implements TenantService {

    @Autowired
    private TenantFeign tenantFeign;

    @Override
    public Boolean findByAccessKeyAndSecurit(String accessKey, String accessSecurit) {
        //1. 根据accessKey, accessSecurit封装查询条件
        TenantDto tenantDto = new TenantDto();
        tenantDto.setAccessKey(accessKey);
        tenantDto.setAccessSecurit(accessSecurit);

        //2. 由于gateway中不允许同步调用, 只允许异步远程调用, 所以开启多线程异步调用
        R<List<QfTenant>> result = null;

        try {
            //开启多线程异步调用
            result = ThreadUtil.execAsync(new Callable<R<List<QfTenant>>>() {
                @Override
                public R<List<QfTenant>> call() throws Exception {
                    // 调用feign接口查询
                    R<List<QfTenant>> result = tenantFeign.findTenantAll(tenantDto);
                    return result;
                }
            }).get();
        } catch (Exception e) {
            e.printStackTrace();
        }


        if (result != null) {
            if (result.getData() != null && result.getData().size() > 0) {
                return true;
            }
        }

        return false;
    }

}
